package com.ithema.controller;

import com.ithema.domain.Order;
import com.ithema.domain.Product;
import com.ithema.service.OrderService;
import com.ithema.service.ProductService;
import com.ithema.util.JsonConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Random;

/**
 * @Description
 * @Author wangyu
 * @Date 2021/10/31
 */
@Slf4j
@RestController
public class OrderController implements JsonConverter {

    @Autowired
    private OrderService orderService;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private ProductService productService;

    @RequestMapping("/order/prod/{pid}")
    public Order order(@PathVariable("pid") Integer pid){
        log.info("pid={}", pid);

        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        log.info("instances={}", toJson(instances));
//        ServiceInstance instance = instances.get(0);
        int index = new Random().nextInt(instances.size());
        ServiceInstance instance = instances.get(index);
        //方法1
//        Product product = restTemplate.getForObject("http://localhost:8081/product/" + pid, Product.class);
        //方法2
//        Product product = restTemplate.getForObject("http://" +instance.getHost()+":"+instance.getPort()+"/product/" + pid, Product.class);
        //方法3
//        Product product = restTemplate.getForObject("http://service-product/product/" + pid, Product.class);
        //方法4
        Product product = productService.findByPid(pid);

        log.info("product={}", toJson(product));

        Order order = new Order();

        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());

        order.setUid(1);
        order.setUsername("wang");
        order.setNumber(1);

        Order orderCreate = orderService.createOrder(order);


        return orderCreate;
    }
    @RequestMapping("/order0/prod/{pid}")
    public Order order0(@PathVariable("pid") Integer pid){
        log.info("pid={}", pid);
        Product product = restTemplate.getForObject("http://localhost:8081/product/" + pid, Product.class);
        log.info("product={}", toJson(product));

        Order order = new Order();
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());

        order.setUid(1);
        order.setUsername("wang");
        order.setNumber(1);

        Order orderCreate = orderService.createOrder(order);


        return orderCreate;
    }

}
